<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>画布直线</title>
    <script type="text/javascript">
        function load() {
            let c = document.getElementById('myCanvas');
            let context = c.getContext('2d');

            // 把画笔抬到(x, y)的地方
            context.moveTo(10, 10);
            context.strokeStyle = '#ff0000';
            context.strokeRect(10, 10, 20, 10);
            // 开始画矩形
            context.stroke();

            context.strokeStyle = '#00ff00';
            context.strokeRect(30, 20, 10, 10);
            context.stroke();

            // 画圆
            context.beginPath();
            context.strokeStyle = '#ff0000';
            context.arc(100, 74, 50, Math.PI / 2, true);
            context.stroke();

            // 实心圆
            context.beginPath();
            context.arc(100, 75, 25, 0, Math.PI / 2, true);
            context.stroke();
            context.closePath();

            // 三角形
            context.beginPath();
            context.moveTo(400, 400);
            context.lineTo(500, 100);
            context.lineTo(400, 200);
            context.closePath();
            context.fill();
        }

        function clearCanvas() {
            let c = document.getElementById('myCanvas');
            let context = c.getContext('2d');
            context.clearRect(0, 0, 800, 400);
        }

        window.onload = function () {
            load();
        }
    </script>
</head>

<body>
    <canvas id="myCanvas" style="border: 1px solid #000000" width="800" height="450">
        你的浏览器不支持canvas元素！
    </canvas>
    <!-- 清空画布 -->
    <input type="button" value="clear" onclick="clearCanvas()" />
</body>

</html>